package com.jair.adb;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

public class AdbCommond {
	/**
	 * 直接拉取
	 */
	public static void sss(){
		Date date = new Date();
		String dateStr = String.valueOf(date.getTime());
		String commond = "E:\\adb\\adb.exe shell screencap -p /sdcard/screenshot"+dateStr+".png";
		String commond2 = "E:\\adb\\adb.exe pull /sdcard/screenshot"+dateStr+".png D:\\test2"+dateStr+".png";//这个耗时间
		try {
			Process p = Runtime.getRuntime().exec("E:\\adb\\adb.exe shell screencap -p " );
			Runtime.getRuntime().exec(commond);
			Thread.sleep(1000);//截图有延时
			Runtime.getRuntime().exec(commond2);
			Thread.sleep(100);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}
	
	
	public static void ScreencapCommon(){
		String commond = "E:\\adb\\adb.exe shell screencap -p";
		try {
			Process process = Runtime.getRuntime().exec(commond);
			InputStream in = process.getInputStream();
			Thread.sleep(1000);
			printHexString(GetImageStrFromIs(in));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void ScreencapToPcCommon(){
		String commond = "E:\\adb\\adb.exe shell screencap -p";
		File f = new File("D:\\test222.png");
		try {
			Process process = Runtime.getRuntime().exec(commond);
			InputStream in = process.getInputStream();
			Thread.sleep(1000);
			inputstreamtofile(in,f);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 *  把文件流写入文件
	 * @param ins
	 * @param file
	 * @throws IOException
	 */
	public  static void inputstreamtofile(InputStream ins,File file) throws IOException{
		   OutputStream os = new FileOutputStream(file);
		   int bytesRead = 0;  
		   byte[] buffer = new byte[1024]; 
		   while ((bytesRead = ins.read(buffer, 0, 1024)) != -1) {
			   //printHexString(buffer);
			  //buffer = fixByte(buffer);
		      os.write(buffer, 0, buffer.length);  
		      buffer = new byte[1024]; 
		   }  
		   os.close();  
		   ins.close();  
		} 
	
	//直接
	 
	
	public static  byte[] GetImageStrFromIs(InputStream in) {
		// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		byte[] data = null;
		// 读取图片字节数组
		try {
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;// 返回Base64编码过的字节数组字符串
	}
	
	
	
	//完全不理解把照片变为base64
	public static String GetImageStrFromPath(String filePathMsg) {
		// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
		InputStream in = null;
		byte[] data = null;
		// 读取图片字节数组
		try {
			in = new FileInputStream(filePathMsg);
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
		return new sun.misc.BASE64Encoder().encode(data);// 返回Base64编码过的字节数组字符串
	}
	
	public static void printHexString( byte[] b) { 
		   for (int i = 0; i < b.length; i++) {   
		     String hex = Integer.toHexString(b[i] & 0xFF);   
		     if (hex.length() == 1) {   
		       hex = '0' + hex;   
		     }   
		     System.out.println(hex.toUpperCase() );   
		   }   
		  
		}

	
//	public static byte[] imageToBytes(String path) throws IOException {  
//	    InputStream inputStream = new FileInputStream(path);  
//	    BufferedInputStream in = new BufferedInputStream(inputStream);  
//	    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);  
//	  
//	    byte[] temp = new byte[10240];  
//	    int size = 0;  
//	    while ((size = in.read(temp)) != -1) {  
//	        out.write(temp, 0, size);  
//	    }  
//	    in.close();  
//	    byte[] content = out.toByteArray();  
//	    return content;  
//	}  
//	public static void ImgToHex(){
//		String file1_1 = "D:\\test31515733913205.png";
//		try {
//			printHexString(imageToBytes(file1_1));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//	}
	
	public static void main(String[] args) throws IOException{
		//sss();
		//ScreencapToPcCommon();
		//fixPhotot();
		//sss();
	}
	

}
